package com.socialsoftware.MediaCommunication.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.socialsoftware.MediaCommunication.model.CallMode;
import com.socialsoftware.MediaCommunication.service.CallService;
import com.socialsoftware.MediaCommunication.service.WebSocketService;
import com.socialsoftware.MediaCommunication.utils.Constants;
import com.socialsoftware.R;


/**
 * Activity for handling incoming calls
 */
public class IncomingCallActivity extends AppCompatActivity {
    private String callerName;
    private String callMode;
    private CallService callService;
    private WebSocketService webSocketService;
    private boolean isCallServiceBound = false;
    private boolean isWebSocketServiceBound = false;

    private final ServiceConnection callServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            CallService.CallServiceBinder binder = (CallService.CallServiceBinder) service;
            callService = binder.getService();
            isCallServiceBound = true;
            
            // Check if we need to automatically answer or reject the call
            if (getIntent().getBooleanExtra(Constants.EXTRA_ANSWER_CALL, false)) {
                answerCall();
            } else if (getIntent().getBooleanExtra(Constants.EXTRA_REJECT_CALL, false)) {
                rejectCall();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            callService = null;
            isCallServiceBound = false;
        }
    };

    private final ServiceConnection webSocketServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            WebSocketService.WebSocketBinder binder = (WebSocketService.WebSocketBinder) service;
            webSocketService = binder.getService();
            isWebSocketServiceBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            webSocketService = null;
            isWebSocketServiceBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incoming_call);
        
        // Get caller information
        callerName = getIntent().getStringExtra(Constants.EXTRA_CALLER_NAME);
        callMode = getIntent().getStringExtra(Constants.EXTRA_CALL_MODE);
        
        if (callMode == null || callMode.isEmpty()) {
            // 如果没有指定通信模式，默认为音频通话
            callMode = CallMode.AUDIO.getValue();
        }
        
        // Set up UI
        TextView tvCallerName = findViewById(R.id.tv_caller_name);
        TextView tvCallType = findViewById(R.id.tv_call_type);
        Button btnAnswer = findViewById(R.id.btn_answer);
        Button btnReject = findViewById(R.id.btn_reject);
        
        tvCallerName.setText(callerName);
        
        // 根据通话模式设置通话类型文本
        String callType;
        if (CallMode.VIDEO.getValue().equals(callMode)) {
            callType = "视频通话";
        } else if (CallMode.SCREEN.getValue().equals(callMode)) {
            callType = "屏幕共享";
        } else {
            callType = "语音通话";
        }
        tvCallType.setText(callType);
        
        btnAnswer.setOnClickListener(v -> answerCall());
        btnReject.setOnClickListener(v -> rejectCall());
        
        // Bind to services
        bindServices();
    }

    private void bindServices() {
        // Bind to CallService
        Intent callServiceIntent = new Intent(this, CallService.class);
        bindService(callServiceIntent, callServiceConnection, Context.BIND_AUTO_CREATE);
        
        // Bind to WebSocketService
        Intent webSocketServiceIntent = new Intent(this, WebSocketService.class);
        bindService(webSocketServiceIntent, webSocketServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private void answerCall() {
        if (callService != null) {
            callService.stopRinging();
            callService.cancelCallNotification();
        }
        
        // 根据通话模式启动不同的Activity
        Intent intent;
        if (CallMode.VIDEO.getValue().equals(callMode)) {
            intent = new Intent(this, VideoChatActivity.class);
        } else if (CallMode.SCREEN.getValue().equals(callMode)) {
            intent = new Intent(this, ScreenSharingActivity.class);
        } else {
            intent = new Intent(this, AudioChatActivity.class);
        }
        
        intent.putExtra(Constants.EXTRA_CALLER_NAME, callerName);
        intent.putExtra(Constants.EXTRA_CALL_MODE, callMode);
        intent.putExtra("isIncoming", true);
        startActivity(intent);
        
        finish();
    }

    private void rejectCall() {
        if (callService != null) {
            callService.stopRinging();
            callService.cancelCallNotification();
        }
        
        if (webSocketService != null && isWebSocketServiceBound && callerName != null) {
            webSocketService.sendRejectCallMessage(callerName);
        }
        
        finish();
    }

    @Override
    protected void onDestroy() {
        if (isCallServiceBound) {
            unbindService(callServiceConnection);
            isCallServiceBound = false;
        }
        
        if (isWebSocketServiceBound) {
            unbindService(webSocketServiceConnection);
            isWebSocketServiceBound = false;
        }
        
        super.onDestroy();
    }
} 